package com.d3works.apps.supplychain.db.repo;

import com.d3works.apps.base.service.app.AppService;
import com.d3works.apps.sale.db.AdvancePayment;
import com.d3works.apps.sale.db.repo.AdvancePaymentSaleRepository;
import com.d3works.apps.supplychain.service.AdvancePaymentServiceSupplychainImpl;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;
import com.google.inject.Inject;
import javax.persistence.PersistenceException;

public class AdvancePaymentSupplychainRepository extends AdvancePaymentSaleRepository {

  @Inject private AppService appService;

  @Override
  public AdvancePayment save(AdvancePayment advancePayment) {
    try {
      if (appService.isApp("supplychain")) {
        Beans.get(AdvancePaymentServiceSupplychainImpl.class).validate(advancePayment);
      }
      return super.save(advancePayment);
    } catch (Exception e) {
      TraceBackService.traceExceptionFromSaveMethod(e);
      throw new PersistenceException(e.getMessage(), e);
    }
  }
}
